import time

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class Base1():
    def __init__(self,driver):
        self.driver = driver

    def open_url(self,url):
        self.driver.get(url)

    def find_element(self,loc):
        return self.driver.find_element(*loc)

    def click(self,loc):
        self.find_element(loc).click()

    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)

    # 三种等待封装：强制等待
    def sleep(self,timeout):
        time.sleep(int(timeout))

    # 三种等待封装：显式等待
    def Webdriverwait(self,loc):
        WebDriverWait(self.driver,20,0.5).until(EC.presence_of_element_located(loc)).click()

    # 三种等待封装：隐式等待
    def implicitly_wait(self,x):
        self.driver.implicitly_wait(x)

    def text(self,loc):
        return self.find_element(loc).text

    # 切换窗口
    def switch_window(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])

    # 滚动条事件：滚动至底部
    def scrollTo_Height(self):
        self.driver.execute_script('window.scrollTo(0,window.document.scrollToHeight)')

    # 滚动条事件：滚动至任意位置
    def scrollTo_x(self,x):
        self.driver.execute_script(f'window.scrollTo(0,{x})')

    # 滚动条事件：滚动至顶部
    def scrollTo_0(self):
        self.driver.execute_script('window.scrollTo(0,0)')

    # 弹框处理：accept
    def alert_accept(self):
        self.driver.switch_to.alert.accept()

    # 弹框处理：dismiss
    def alert_dismiss(self):
        self.driver.switch_to.alert.dismiss()

    # 弹框处理：弹框文本处理accept
    def alert_text_accept(self,val):
        a = self.driver.switch_to.alert
        a.send_keys(val)
        a.accept()

    # 弹框处理：弹框文本处理dismiss
    def alert_text_dismiss(self,val):
        a = self.driver.switch_to.alert
        a.send_keys(val)
        a.dismiss()

    # frame框架：进入框架
    def frame(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))

    # frame框架：退出框架
    def frame_quit(self):
        self.driver.switch_to.default_content()

    # 鼠标事件：双击
    def double_click(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).perform()

    # 鼠标事件：三击
    def three_click(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).click().perform()

    # 鼠标事件：拖拽
    def drag_and_drop(self,loc1,loc2):
        ActionChains(self.driver).drag_and_drop(self.find_element(loc1),self.find_element(loc2)).perform()

    # 鼠标事件：悬停
    def move_to_element(self,loc):
        ActionChains(self.driver).move_to_element(self.find_element(loc)).perform()

    # 鼠标事件：右击
    def context_click(self,loc):
        ActionChains(self.driver).context_click(self.find_element(loc)).perform()

    # 键盘事件：回车
    def enter(self,loc):
        a = self.find_element(loc)
        a.send_keys(Keys.ENTER)

    # 键盘事件：esc
    def esc(self,loc):
        a = self.find_element(loc)
        a.send_keys(Keys.ESCAPE)

    # 键盘事件：space
    def space(self, loc):
        a = self.find_element(loc)
        a.send_keys(Keys.SPACE)

    # 键盘事件：control+c
    def control_c(self, loc):
        a = self.find_element(loc)
        a.send_keys(Keys.CONTROL,'c')

    # 键盘事件：control+v
    def control_v(self, loc):
        a = self.find_element(loc)
        a.send_keys(Keys.CONTROL,'v')

    # 键盘事件：control+x
    def control_x(self, loc):
        a = self.find_element(loc)
        a.send_keys(Keys.CONTROL,'x')

    # 键盘事件：control+a
    def control_a(self, loc):
        a = self.find_element(loc)
        a.send_keys(Keys.CONTROL,'a')

    # 下拉框事件：方法1
    def select_value(self,loc,x):
        a = Select(self.find_element(loc))
        a.select_by_value(x)

    # 下拉框事件：方法2
    def select_index(self,loc,x):
        a = Select(self.find_element(loc))
        a.select_by_index(x)

    # 下拉框事件：方法3
    def select_text(self, loc, x):
        a = Select(self.find_element(loc))
        a.select_by_visible_text(x)

    # 带时间戳的截屏
    def screenshot(self):
        st = time.strftime('%Y%m%d%H%M',time.localtime())
        self.driver.save_screenshot(st + '.png')